﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using OSGeo.MapGuide;

namespace IGIS
{


    /// <summary>
    /// Summary description for SiteConnection
    /// </summary>
    public class SiteUtilities
    {
        private string mUserName = "Administrator";
        private string mPassword = "admin";
        private MapSettings mMapSettings;

        private MgSiteConnection mSiteConnection;
        private MgResourceService mResourceService;
        private MgFeatureService mFeatureService;
        private MgMap mMap;
        private MgSelection mSelection;

        #region Usable objects
        public utilityfunctions Utils = new utilityfunctions();
        public MgUserInformation UserInfo
        {
            get
            {
                if (HttpContext.Current.Session["SESSION"] == null)
                    return new MgUserInformation(mUserName, mPassword);
                else
                {
                    string sessionId = HttpContext.Current.Session["SESSION"] as string;
                    return new MgUserInformation(sessionId);
                }
            }
        }
        public MgSiteConnection SiteConnection
        {
            get { return mSiteConnection; }
        }
        public MgResourceService ResourceService
        {
            get
            {
                if (mResourceService == null)
                {
                    mResourceService = mSiteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;
                }
                return mResourceService;
            }
        }
        public MgFeatureService FeatureService
        {
            get
            {
                if (mFeatureService == null)
                {
                    mFeatureService = mSiteConnection.CreateService(MgServiceType.FeatureService) as MgFeatureService;
                }
                return mFeatureService;
            }
        }
        public MgMap Map
        {
            get
            {
                if (mMap == null)
                {
                    mMap = new MgMap(mSiteConnection);
                    mMap.Open(ResourceService, mMapSettings.MapName);
                }
                return mMap;
            }
        }
        public MgSelection Selection
        {
            get
            {
                if (mSelection == null)
                {
                    mSelection = new MgSelection(Map);
                    mSelection.Open(ResourceService, Map.Name);
                }
                return mSelection;
            }
        }
        #endregion

        public SiteUtilities(MapSettings settings)
        {
            Utils.InitializeWebTier();
            mMapSettings = settings;
            mSiteConnection = new MgSiteConnection();
            SiteConnection.Open(UserInfo);
            if (HttpContext.Current.Session["SESSION"] == null)
            {
                HttpContext.Current.Session["SESSION"] = SiteConnection.GetSite().CreateSession();
            }
        }
    }
}